# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Martell Malone <martellmalone@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

pkgname=pacman
pkgver=5.2.2
pkgrel=18
pkgdesc="A library-based package manager with dependency support (MSYS2 port)"
arch=('i686' 'x86_64')
url="https://www.archlinux.org/pacman/"
license=('GPL')
groups=('base-devel')
depends=('bash>=4.2.045'
         'gettext'
         'gnupg'
         'curl'
         'pacman-mirrors'
         'msys2-keyring'
         'which'
         'bzip2'
         'xz'
         'zstd')
checkdepends=('python')
makedepends=('asciidoc'
             'doxygen'
             'git'
             'meson'
             'ninja'
             'gettext-devel'
             'heimdal-devel'
             'libarchive-devel'
             'libcurl-devel'
             'libgpgme-devel'
             'libsqlite-devel'
             'libunistring-devel')
backup=("etc/pacman.conf"
        "etc/makepkg.conf"
        "etc/makepkg_mingw.conf")
source=(https://sources.archlinux.org/other/pacman/${pkgname}-${pkgver}.tar.gz
        "pacman.conf"
        "makepkg.conf"
        "makepkg_mingw.conf"
        "makepkg-mingw"
        "0000-pacman-msysize.patch"
        "0001-more-debugging-info.patch"
        "0002-pacman-core-update.patch"
        "0003-remove-ldconfig.patch"
        "0004-change-epoch-sep.patch"
        "0006-makepkg-avoid-creating-.tar-files-with-extended-attr.patch"
        "0007-exe-interp-ignore-file-conflict.patch"
        "0008-answer-yes-by-default.patch"
        "0009-msys-use-pipe-instead-socket.patch"
        "0010-filelist-strcasecmp.patch"
        "0011-update-ja-po-for-msys2.patch"
        "0012-workaround-bsdtar-AD-account.patch"
        "0013-use-libintl.patch"
        "0014-fix-asciidoc-argparse.patch"
        "0016-excise-fakeroot.patch"
        "0017-excise-sudo.patch"
        "0018-use-msys-tools.patch"
        "0019-doxyfile-in-missing.patch")
validpgpkeys=('6645B0A8C7005E78DB1D7864F99FFE0FEAE999BD'  # Allan McRae <allan@archlinux.org>
              'B8151B117037781095514CA7BBDFFC92306B1121') # Andrew Gregory (pacman) <andrew@archlinux.org>
sha256sums=('bb201a9f2fb53c28d011f661d50028efce6eef2c1d2a36728bdd0130189349a0'
            '11aa1895cda0522672a23879ceaa63054c2865bf775ee3b20f582bfa51ce1437'
            '822af13248f04690377cd193370f33ac2f00a41234ead9cf3b7e5d5aba8bd0c5'
            'b9967df8ff800f121dfe3bb9ecd99433f7d8a96550f843c7baf1f4f1ec77f30c'
            'fc7b333b90dacd0e7b760a2519cc742ec802753adf9ceea723fdb6ee043ff098'
            '2e2dde573b971011abad5340cb2cd5a4b9a0e8470a2ed94d7a969525e0bed0c1'
            '24ea2c8dca37847e04894ebfd05d1cf5df49dc0c8089f5581c99caa19b77a7ef'
            'abb81e2dc9507dc20dc860287b8ce1bf5e5cd7cec3dd386770dc568c39db6431'
            'fa6056f2d998578f8e11a97adf4d8a7bb4bba6f75b65aa498c82198ed9da9841'
            'b672f3648bbfd7763b1e689c73a9ba7ddc230016a0d8dfeebca1b7faf7777424'
            'e2731c4007a7c78848679e2203c35d1e56d3ef15a7586aeac9cc9e6e97824cfb'
            '52343bbf99da6408950e34dd84319e34724040f866c497c25e36b4ee54030e82'
            'e4f6e17af19e17e745a9f1c6b8402f5896229062c82167cb61f8e7d29eda716c'
            '9e8fe5ee78192b0407e80ad2e52cb27569c35974b6c26e465e3d55e19c03d108'
            '93be4523fb8c3dd6b56eddfe0b09e666725a62eb43392fee336ba1a328f9ffdd'
            '558919a5e4632512fa42690bd5ea96e72766213ae6943408f7010daa486de929'
            'c242202f3526d5eaa21d321a1d0fa472697c50e8288175ebe93212356c78d1f1'
            '7cec4688b83df9f8ab56f5ab5e162383fe8928ff88b76d753a2cea935f30ec93'
            'b84310fd8fb9a98258c55ed9628226b14f55d6e42304df4d2a5bd8d8445c03b3'
            'd6b6accddc890aff38b5ded3300e9dde35e1d7ed3f767e1655772e2cc7871739'
            '8346a5799be41bd3524fa6fdc57b6175d9d6a00b366f99bd5cd7fa3d43d0ae98'
            '7f60108a372718cfec5d883167a33983be7c5df33fc48bfc21f664449ac7a0a4'
            '43eb9548ddff92fb08c0c7636c4978541ff225e220bf4ba6512118cf75e76b07')

prepare() {
  cd ${srcdir}/${pkgname}-${pkgver}
  patch -p1 -i ${srcdir}/0000-pacman-msysize.patch
  patch -p1 -i ${srcdir}/0001-more-debugging-info.patch
  patch -p1 -i ${srcdir}/0002-pacman-core-update.patch
  patch -p1 -i ${srcdir}/0003-remove-ldconfig.patch
  patch -p1 -i ${srcdir}/0004-change-epoch-sep.patch
  patch -p1 -i ${srcdir}/0006-makepkg-avoid-creating-.tar-files-with-extended-attr.patch
  patch -p1 -i ${srcdir}/0007-exe-interp-ignore-file-conflict.patch
  patch -p1 -i ${srcdir}/0008-answer-yes-by-default.patch
  patch -p1 -i ${srcdir}/0009-msys-use-pipe-instead-socket.patch
  patch -p1 -i ${srcdir}/0010-filelist-strcasecmp.patch
  patch -p1 -i ${srcdir}/0011-update-ja-po-for-msys2.patch
  patch -p1 -i ${srcdir}/0012-workaround-bsdtar-AD-account.patch
  patch -p1 -i ${srcdir}/0013-use-libintl.patch
  patch -p1 -i ${srcdir}/0014-fix-asciidoc-argparse.patch
  patch -p1 -i ${srcdir}/0016-excise-fakeroot.patch
  patch -p1 -i ${srcdir}/0017-excise-sudo.patch
  patch -p1 -i ${srcdir}/0018-use-msys-tools.patch
  patch -p1 -i ${srcdir}/0019-doxyfile-in-missing.patch
}

build() {
  cd ${srcdir}/${pkgname}-${pkgver}

  LDFLAGS+=" -static-libgcc" \
  meson build-${CARCH} \
    --buildtype=plain \
    --prefix=/usr \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --default-library=static \
    -Dbuildstatic=true \
    -Ddoc=enabled \
    -Ddoxygen=enabled \
    -Dgpgme=enabled \
    -Dcurl=enabled \
    -Duse-git-version=false \
    -Dpkg-ext=.pkg.tar.xz \
    -Dscriptlet-shell=/usr/bin/bash

  meson compile -C build-${CARCH}
}

check() {
  cd ${srcdir}/${pkgname}-${pkgver}
  meson test -C build-${CARCH}
}

package() {
  cd ${srcdir}/${pkgname}-${pkgver}
  DESTDIR=${pkgdir} meson install -C build-${CARCH}

  # install Arch specific stuff
  install -dm755 ${pkgdir}/etc
  install -m644 ${srcdir}/pacman.conf ${pkgdir}/etc/pacman.conf

  case "${CARCH}" in
  i686)
    mycarch="i686"
    mychost="i686-pc-msys"
    myflags="-march=i686"
  ;;
  x86_64)
    mycarch="x86_64"
    mychost="x86_64-pc-msys"
    myflags="-march=x86-64"
  ;;
  esac

  install -m644 ${srcdir}/makepkg.conf ${pkgdir}/etc/
  install -m644 ${srcdir}/makepkg_mingw.conf ${pkgdir}/etc/
  install -m755 ${srcdir}/makepkg-mingw ${pkgdir}/usr/bin/

  # set things correctly in the default conf file
  sed -i ${pkgdir}/etc/makepkg.conf \
    -e "s|@CARCH[@]|${mycarch}|g" \
    -e "s|@CHOST[@]|${mychost}|g" \
    -e "s|@CARCHFLAGS[@]|${myflags}|g"
}
